<html>
<head><meta charset="utf-8"><title>Stability of Arc::as_ptr · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Stability.20of.20Arc.3A.3Aas_ptr.html">Stability of Arc::as_ptr</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="208101432"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Stability%20of%20Arc%3A%3Aas_ptr/near/208101432" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Stability.20of.20Arc.3A.3Aas_ptr.html#208101432">(Aug 26 2020 at 14:48)</a>:</h4>
<p>Hello! How stable is the <code>Arc::as_ptr</code> method? As long as the strong_count is &gt;= than one (ie: there is still at least one Arc alive), the ptr value will be the same for all Arc's cloned? I was considering getting the pointer of the Arc, coercing to usize and using that value as an id to index in a <code>HashMap</code>, I would never actually de-reference that pointer, just it as an usize id. </p>
<p>I've been reading about <code>Pin</code> but I am not sure I understand if that helps me or not.</p>



<a name="208104508"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Stability%20of%20Arc%3A%3Aas_ptr/near/208104508" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Stability.20of.20Arc.3A.3Aas_ptr.html#208104508">(Aug 26 2020 at 15:08)</a>:</h4>
<p>Why do you want to do that? (It seems like it would work to me, but I am no expert.)</p>



<a name="208105217"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Stability%20of%20Arc%3A%3Aas_ptr/near/208105217" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Stability.20of.20Arc.3A.3Aas_ptr.html#208105217">(Aug 26 2020 at 15:13)</a>:</h4>
<p>Agree with the "why". It sounds like you should just have an <code>AtomicUsize</code> or a thread-local usize. See <a href="https://stackoverflow.com/q/32935808/155423">Generate sequential IDs for each instance of a struct</a></p>



<a name="208105481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Stability%20of%20Arc%3A%3Aas_ptr/near/208105481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Stability.20of.20Arc.3A.3Aas_ptr.html#208105481">(Aug 26 2020 at 15:14)</a>:</h4>
<p>But AFAIK, it wouldn't be possible for the pointer to change because the Arc information is stored alongside it.</p>



<a name="208106593"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Stability%20of%20Arc%3A%3Aas_ptr/near/208106593" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Stability.20of.20Arc.3A.3Aas_ptr.html#208106593">(Aug 26 2020 at 15:22)</a>:</h4>
<p>For a long lived program, how do you make sure id's are not re-used?</p>



<a name="208106994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Stability%20of%20Arc%3A%3Aas_ptr/near/208106994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Stability.20of.20Arc.3A.3Aas_ptr.html#208106994">(Aug 26 2020 at 15:25)</a>:</h4>
<p>If you are worried about reuse, I'd think your example is less complete. If you allocate the Arc, deallocate it, then allocate another, it's possible that the memory allocator gives you the same address.</p>



<a name="208107271"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Stability%20of%20Arc%3A%3Aas_ptr/near/208107271" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Stability.20of.20Arc.3A.3Aas_ptr.html#208107271">(Aug 26 2020 at 15:26)</a>:</h4>
<p>If I did my math right, on 64-bit, you could generate a new ID every nanosecond for 584.6 years.</p>



<a name="208107624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Stability%20of%20Arc%3A%3Aas_ptr/near/208107624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Stability.20of.20Arc.3A.3Aas_ptr.html#208107624">(Aug 26 2020 at 15:29)</a>:</h4>
<p>on 32-bit, that's only 4 seconds. (man, exponents)</p>



<a name="208107700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Stability%20of%20Arc%3A%3Aas_ptr/near/208107700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Stability.20of.20Arc.3A.3Aas_ptr.html#208107700">(Aug 26 2020 at 15:29)</a>:</h4>
<p>but</p>



<a name="208107798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Stability%20of%20Arc%3A%3Aas_ptr/near/208107798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Stability.20of.20Arc.3A.3Aas_ptr.html#208107798">(Aug 26 2020 at 15:30)</a>:</h4>
<p>if this were keys to a hashmap, then you'd have things in the hashmap, presumably</p>



<a name="208107842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Stability%20of%20Arc%3A%3Aas_ptr/near/208107842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Stability.20of.20Arc.3A.3Aas_ptr.html#208107842">(Aug 26 2020 at 15:30)</a>:</h4>
<p>and you'd run out of memory / address space well before you hit the end of the keys</p>



<a name="208108462"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Stability%20of%20Arc%3A%3Aas_ptr/near/208108462" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Stability.20of.20Arc.3A.3Aas_ptr.html#208108462">(Aug 26 2020 at 15:35)</a>:</h4>
<p>Fair enough</p>



<a name="208125669"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Stability%20of%20Arc%3A%3Aas_ptr/near/208125669" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Stability.20of.20Arc.3A.3Aas_ptr.html#208125669">(Aug 26 2020 at 17:58)</a>:</h4>
<p>I'm using arc pointers into a hashmap in my own codebase for exactly this purpose, and I had issues with clashing keys due to deallocation, so now my hashmap stores <code>HashMap&lt;*const K, (Arc&lt;K&gt;, V)&gt;</code> where the <code>Arc&lt;K&gt;</code> is a clone of the pointer that was derived for the key. That keeps it alive (and pinned) as long as the key exists in the hashmap</p>



<a name="208125778"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Stability%20of%20Arc%3A%3Aas_ptr/near/208125778" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Stability.20of.20Arc.3A.3Aas_ptr.html#208125778">(Aug 26 2020 at 17:59)</a>:</h4>
<p>It would be nicer to store <code>HashMap&lt;Arc&lt;K&gt;, V&gt;</code> but this requires a special wrapper around the arc so that it gets the <code>Hash + Eq</code> implementation from the pointer instead of the underlying key type</p>



<a name="208128463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Stability%20of%20Arc%3A%3Aas_ptr/near/208128463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Stability.20of.20Arc.3A.3Aas_ptr.html#208128463">(Aug 26 2020 at 18:22)</a>:</h4>
<p>I'd follow <a href="https://stackoverflow.com/q/41035869/155423">How to use a struct's member as its own key when inserting the struct into a map without duplicating it?</a></p>



<a name="208128524"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Stability%20of%20Arc%3A%3Aas_ptr/near/208128524" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Stability.20of.20Arc.3A.3Aas_ptr.html#208128524">(Aug 26 2020 at 18:22)</a>:</h4>
<p>which, yes, means you need to implement <code>Hash</code> and <code>Eq</code> by delegating to the pointer</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>